package mest.controller.Modeling;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import mest.VO.Modeling.BomSaveFile_manytomany.savefileVO;
import mest.VO.Modeling.WorkrouteWork_manytomany.workVO;
import mest.entity.bomsave_manytomany;
import mest.entity.workroutework_manytomany;
import mest.service.Modeling.IbomService;
import mest.util.mestUtil;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import mest.entity.bom;

/**
 * @Description
 * @auther Machenike
 * @create 2019-11-15 22:41
 */
@RestController
@CrossOrigin
@RequestMapping(value = "/bom")
public class bomController {

    @Resource
    private IbomService ibomService;

    @RequestMapping(value = "/insert")
    public String insert(mest.entity.bom wf, HttpServletRequest request) {
        String theirDb = mestUtil.getCompanyDB(request);
        return ibomService.insertBOMFile(wf, theirDb);
    }

    @RequestMapping(value = "/baseSelect", produces = {"application/json;charset=utf-8", "text/plain;charset=utf-8"})
    public String baseSelect(int index, int pageSize, HttpServletRequest request) {
        String theirDb = mestUtil.getCompanyDB(request);
        List<mest.entity.bom> BOMPage = ibomService.findBOMFilePage(index, pageSize,theirDb);
        int i = ibomService.selectCount(theirDb);
        Map map = new HashMap();
        map.put("code", 0);
        map.put("msg", "");
        map.put("count", i);
        map.put("data", BOMPage);
        return JSON.toJSONString(map);
    }

    //    toolbar查看bom详情
    @RequestMapping(value = "/saveFileDetail", produces = {"application/json;charset=utf-8", "text/plain;charset=utf-8"})
    public String saveFileDetail(int bomid, int index, int pageSize, HttpServletRequest request) {
        String theirDb = mestUtil.getCompanyDB(request);
        System.out.println(bomid);
        List<savefileVO> savefileByBOMId = ibomService.findSaveByBOMId(bomid, index, pageSize, theirDb);
        int i = ibomService.SaveCount(bomid, theirDb);
        Map map = new HashMap();
        map.put("code", 0);
        map.put("msg", "");
        map.put("count", i);
        map.put("data", savefileByBOMId);
        return JSON.toJSONString(map);
    }

    //    toolbar添加bom
    @RequestMapping(value = "/addBom", produces = {"application/json;charset=utf-8", "text/plain;charset=utf-8"})
    public String addBom(bomsave_manytomany bsmtm, HttpServletRequest request) {
        String theirDb = mestUtil.getCompanyDB(request);
        int i = ibomService.insertManytomany_bomAndSave(bsmtm, theirDb);
        if (i >= 1) {
            return JSON.toJSONString("添加成功");
        } else {
            return JSON.toJSONString("添加失败");
        }
    }


    // toolbar 修改bom
    @RequestMapping(value = "/updateBom", produces = {"application/json;charset=utf-8", "text/plain;charset=utf-8"})
    public String updateBom(savefileVO bsmtm, HttpServletRequest request) {
        String theirDb = mestUtil.getCompanyDB(request);
        int i = ibomService.updateManytomany_bomAndSave(bsmtm, theirDb);
        if (i >= 1) {
            return JSON.toJSONString("修改成功");
        } else {
            return JSON.toJSONString("修改失败");
        }
    }

    // toolbar 删除bom
    @RequestMapping(value = "/delBom", produces = {"application/json;charset=utf-8", "text/plain;charset=utf-8"})
    public String delBom(bomsave_manytomany bsmtm, HttpServletRequest request) {
        String theirDb = mestUtil.getCompanyDB(request);
        int i = ibomService.delManytomany_bomAndSave(bsmtm, theirDb);
        if (i >= 1) {
            return JSON.toJSONString("删除成功");
        } else {
            return JSON.toJSONString("删除失败");
        }
    }

    //根据bomid和存货id查询是否已在多对多表存在

    @RequestMapping("/examine")
    public String examine(@RequestBody JSONObject json, HttpServletRequest request){
        String theirDb = mestUtil.getCompanyDB(request);
        Map<String,Object> map = json.toJavaObject(Map.class);
        map.put("theirDb",theirDb);
        return ibomService.examine(map);
    }


}
